/*
	Copyright (C) 2011 Salil Bhagurkar

	This file is part of illusion

	illusion is free software: you can redistribute it and/or modify
	it under the terms of the GNU Lesser General Public License as
	published by the Free Software Foundation, either version 3 of
	the License, or (at your option) any later version.

	illusion is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Lesser General Public License for more details.

	You should have received a copy of the GNU Lesser General Public
	License along with illusion. 
	If not, see <http://www.gnu.org/licenses/>.
*/


#include <klib/lib.h>
#include <kernel/ilapp.h>
#include <kernel/errors.h>
#include <apimod/apimod.h>
#include <kernel/vfs.h>
#include <kernel/input.h>
#include <kernel/ilapp_base.h>
#include <kernel/graphics.h>
#include <kernel/ilg.h>

struct apimod_console *console;
struct apimod_fs1 *fs1;
struct apimod_fs *fs;
struct apimod_graphics *g;
struct apimod_lib_memory *m;

int ilapp_main()
{
	GETI("console", &console);
	GETI("fs1", &fs1);
	GETI("fs", &fs);
	GETI("graphics", &g);
	GETI("memory", &m);

	int err;
	int fx = 800, fy = 600;

	err = g->initialize(800, 600);



	uint len;

	uint bgnd_size = 800 * 600 * 4 + sizeof(struct ilg_header);
	struct ilg_header *ilg_bgnd = (struct ilg_header *)m->malloc(bgnd_size);
	ilg_bgnd->left = 0;
	ilg_bgnd->top = 0;
	ilg_bgnd->height = 600;
	ilg_bgnd->width = 800;
	m->copy(ilg_bgnd->sign, ILG_SIGN, ILG_SIGN_LEN);
	m->set(ilg_bgnd->pixels, 255, 800 * 600 * 4);

	fs1->create_file("graphics:", "ginit_bgnd");
	fs1->write_all_file("graphics:/ginit_bgnd", (char *)ilg_bgnd, bgnd_size);

	m->free(ilg_bgnd, bgnd_size);


	struct ilg_header *ilg = (struct ilg_header *)fs1->read_all_file(&len,
			"illusion:/rdisk/default_faunt.ilg");

	//ilg->left = fx / 2 - ilg->width / 2;
	//ilg->top = fy / 2 - ilg->height / 2;
	ilg->left = 0;
	ilg->top = 0;
	//console->printf("It is %u x %u\n", ilg->width, ilg->height);


	fs1->create_file("graphics:", "ginit_img");

	fs1->write_all_file("graphics:/ginit_img", (char *)ilg, len);
	g->flush();


	m->free(ilg, len);

	char *data = fs1->read_all_file(&len, "sys:/graphics");
	m->free(data, len);

	g->deinitialize();


	return 0;
}
